﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Eis51.CampanyCMS.Model.BaseInfoMng;
using Eis51.CampanyCMS.Business.BaseInfoMng;
using Eis51.CampanyCMS.Model;
using Eis51.CampanyCMS.FrameWork;
using Eis51.CampanyCMS.Business.Bus;
using System.IO;
using Eis51.Web.Admin;

namespace Eis51.CampanyCMS.WebUI.Admin.Baseinfo
{
    public partial class homepicsedit : BasePage
    {
        private int Seqno
        {
            get
            {
                if (Request.QueryString["Seqno"] != null)
                {
                    return int.Parse(Request.QueryString["Seqno"]);
                }
                return 0;
            }

        }



        private string Type
        {
            get
            {
                if (Request["Type"] != null)
                {
                    return Request["Type"];
                }
                return "F";
            }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ShowData();
            }
        }

        private void ShowData()
        {

            BaseHomePics c = null;
            if (this.Seqno > 0)
            {
                c = BaseHomePicsBLL.GetInstance().Get(new BaseHomePics.Key(this.Seqno));
                if (c != null)
                {
                    UControl.CopyEntityToControl(this.Page.Form, c);
                    if (!string.IsNullOrEmpty(c.Filename))
                    {
                        //FILENAME[（固定在/UploadFile/|Createby|/Img/|FILENAME|）]
                        imgShow.ImageUrl = string.Format("~/UploadFile/{0}/Img/{1}", AppContext.CurrentAdministrator.UserName, c.Filename);
                    }
                    btnDelFile.Visible = !string.IsNullOrEmpty(c.Filename);
                }
            }
        }

        private string SaveFile()
        {            
            string savePath = string.Format("{0}/{1}/{2}"
                , DateTime.Now.ToString("yyyy")
                , DateTime.Now.ToString("MM")
                , DateTime.Now.ToString("dd"));

            string sss = UploadFile.PostedFile.FileName;
            string patch = Server.MapPath(string.Format("~/UploadFile/{0}/Img/{1}", AppContext.CurrentAdministrator.UserName, savePath));
            string fileName = FileListBLL.SaveFile(UploadFile, patch);
            return Path.Combine(savePath, fileName);
        }

        private bool Save()
        {
            string filename = string.Empty;
            if (UploadFile.PostedFile.FileName != string.Empty)
            {
                FileInfo ff = new FileInfo(UploadFile.PostedFile.FileName);
                if (!(ff.Extension.ToLower() == ".gif" ||
                    ff.Extension.ToLower() == ".jpeg" ||
                    ff.Extension.ToLower() == ".bmp" ||
                    ff.Extension.ToLower() == ".jpg"
                    ))
                {
                    BaseTools.ShowMessage(this, "请上传图片文件！");
                    return false;
                }

                filename = SaveFile();
            }

            
            BaseHomePics c = null;
            if (this.Seqno > 0)
            {
                c = BaseHomePicsBLL.GetInstance().Get(new BaseHomePics.Key(this.Seqno));

            }
            else
            {
                c = new BaseHomePics();

            }

            UControl.CopyControlToEntity(this.Page.Form, c);
            if (filename != string.Empty)
            {
                c.Filename = filename;
            }
            if (c.BaseHomePicsId == 0)
            {
                c.Createby = AppContext.CurrentAdministrator.UserName;
                c.Createdate = DateTime.Now;
                c.Licd = AppContext.LCID;
                c.Type = this.Type;
                
            }
            BaseHomePicsBLL.GetInstance().Submit(c);
            return true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Save())
            {
                Response.Redirect("homePicslist.aspx?Type=" + this.Type);
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("homePicslist.aspx?Type=" + this.Type);
        }

        protected void btnDelFile_Click(object sender, EventArgs e)
        {
            BaseHomePics obj = BaseHomePicsBLL.GetInstance().Get(new BaseHomePics.Key((Seqno)));
            //File_path1[文件地址1（固定在/UploadFile/|Createby|/File/|File_path1|）]
            string path = Server.MapPath("~/UploadFile/" + obj.Createby + "/Img/" + obj.Filename);
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            imgShow.ImageUrl = "";            
            imgShow.Visible = false;
            btnDelFile.Visible = false;
            if (obj != null)
            {
                obj.Filename = "";
                BaseHomePicsBLL.GetInstance().Submit(obj);
            }        
        }
    }
}
